#!/usr/bin/env node

const http = require('http'),
  fs = require('fs'),
  cheerio = require('cheerio');
  
http.createServer((req,res) => { 
  //console.log(`${req.method} ${req.url} ${req.httpVersion}`);

  const htmlContent = fs.readFileSync('./news.html');
  const $ = cheerio.load(htmlContent,{decodeEntities: false});

  var result = [];
  let news = $('.news-model');

  news.map(function(){
    var content = {};
    var title = $(this).find('a').text();
    var cTime = $(this).find('span').text().slice(1,-1);

    content.newsTitle = title;
    content.createTime = cTime;
    //console.log(content);

    if(content.createTime.split('-')[0] === '2018'){
      console.log(content.createTime.split('-')[0]);
      result.push(content);
    }
    console.log(content);
  });
  //console.log(content);//
  //var r = result.toString('utf8'); 
  res.setHeader('Content-Type','text/json');
  
  res.end((JSON.stringify(result)));
  
}).listen(8080);




